<?php
namespace Illuminate\Session;

use Illuminate\Support\Manager;

class SessionManager extends Manager {
    protected function callCustomCreator($driver) {
        return $this->buildSession(parent::callCustomCreator($driver));
    }
    protected function createArrayDriver() {
        return $this->buildSession(new NullSessionHandler);
    }
    protected function createCookieDriver() {
        return $this->buildSession(new CookieSessionHandler(
            $this->app['cookie'], $this->app['config']['session.lifetime']
        ));
    }

    protected function createFileDriver() {
        return $this->createNativeDriver();
    }
    protected function createNativeDriver() {
        $lifetime = $this->app['config']['session.lifetime'];

        return $this->buildSession(new FileSessionHandler(
            $this->app['files'], $this->app['config']['session.files'], $lifetime
        ));
    }

    protected function createDatabaseDriver() {
        $table = $this->app['config']['session.table'];
        $lifetime = $this->app['config']['session.lifetime'];

        return $this->buildSession(new DatabaseSessionHandler(
            $this->getDatabaseConnection(), $table, $lifetime, $this->app
        ));
    }
    protected function getDatabaseConnection() {
        $connection = $this->app['config']['session.connection'];

        return $this->app['db']->connection($connection);
    }

    protected function createApcDriver() { // apc
        return $this->createCacheBased('apc');
    }
    protected function createMemcachedDriver() { // memcached
        return $this->createCacheBased('memcached');
    }
    protected function createRedisDriver() { // redis
        $handler = $this->createCacheHandler('redis');

        $handler->getCache()->getStore()->setConnection( // 设置连接
            $this->app['config']['session.connection']
        );

        return $this->buildSession($handler);
    }
    protected function createCacheBased($driver) { // 缓存
        return $this->buildSession($this->createCacheHandler($driver));
    }
    protected function createCacheHandler($driver) {
        $store = $this->app['config']->get('session.store') ?: $driver; // 驱动

        return new CacheBasedSessionHandler(
            clone $this->app['cache']->store($store), // cache
            $this->app['config']['session.lifetime'] // 生存时间
        );
    }
    protected function buildSession($handler) { // 保存 Session
        if ( $this->app['config']['session.encrypt'] ) return $this->buildEncryptedSession($handler);

        return new Store($this->app['config']['session.cookie'], $handler);
    }
    protected function buildEncryptedSession($handler) { // 加密保存 Session
        return new EncryptedStore(
            $this->app['config']['session.cookie'], $handler, $this->app['encrypter']
        );
    }

    public function getSessionConfig() { // 配置
        return $this->app['config']['session'];
    }
    public function getDefaultDriver() { // 默认驱动
        return $this->app['config']['session.driver'];
    }
    public function setDefaultDriver($name) { // 设置默认驱动
        $this->app['config']['session.driver'] = $name;
    }
}
